package br.zeos;

import java.sql.ResultSet;
import java.util.HashMap;
import br.zeos.ZSql.Tipos;

public class ZTable {

    private ZConnect Connection = null;
    private String TableName = "";
    private HashMap<String, Tipos> Fields = new HashMap<String, Tipos>();
    private String PrimaryKeyName = "";

    public ZTable() {

    }

    

    public ZTable(ZConnect Connection, String TableName) {
        try {
            this.Connection = Connection;
            this.TableName = TableName;
            String catalog = null;
            String schemaPattern = null;
            String tableNamePattern = this.TableName;
            String columnNamePattern = null;
            //Colocar % tabela % para capturar todas as tabelas
            ResultSet result = Connection.getConexao().getMetaData().getColumns(
                    catalog, schemaPattern, "%"+tableNamePattern+"%", columnNamePattern);
            int linha = 0;
            while (result.next()) {
                if (!true) {
                    for (int i = 0; i < result.getMetaData().getColumnCount(); i++) {                
                        System.out.println("result[" + linha + "]['" + result.getMetaData().getColumnName(i + 1) + "'] = " + result.getString(i + 1));
                    }
                    System.out.println("----");
                }
                linha++;
                String nome = result.getString("COLUMN_NAME").toString().toUpperCase();
                String tipo = result.getString("TYPE_NAME").toString().toUpperCase();
                Boolean ai = false;
                if (!this.getConnection().getProtocol().equalsIgnoreCase("sqlite")) {
                    ai = (result.getString("IS_AUTOINCREMENT").toString().toUpperCase().equals("YES")) ? true : false;
                } else {
                    ai = nome.equalsIgnoreCase("id");
                }
                Tipos ClassTipo = null;
                if (tipo.contains("CHAR") || tipo.contains("TEXT")) {
                    ClassTipo = Tipos.String;
                } else if (tipo.contains("INT") || tipo.contains("NUMBER")) {
                    ClassTipo = Tipos.Integer;
                } else if (tipo.contains("FLOAT") || tipo.contains("NUMERIC")) {
                    ClassTipo = Tipos.Float;
                } else if (tipo.contains("TIMESTAMP") || tipo.contains("DATETIME") || tipo.contains("HOUR")) {
                    ClassTipo = Tipos.Calendar;                
                } else if (tipo.contains("BLOB") || tipo.contains("BYTE") ) {
                    ClassTipo = Tipos.Blob;
                } else {
                    ClassTipo = Tipos.String;
                }
                if (ai) {
                    this.PrimaryKeyName = nome;
                }
                this.Fields.put(nome, ClassTipo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public ZConnect getConnection() {
        return Connection;
    }

    public void setConnection(ZConnect Connection) {
        this.Connection = Connection;
    }

    public String getTableName() {
        return TableName;
    }

    public void setTableName(String TableName) {
        this.TableName = TableName;
    }

    public HashMap<String, Tipos> getFields() {
        return Fields;
    }

    public void setFields(HashMap<String, Tipos> Fields) {
        this.Fields = Fields;
    }

    public void setField(String Name, Tipos Type) {
        this.Fields.put(Name, Type);
    }

    public String getPrimaryKeyName() {
        return PrimaryKeyName;
    }

    public void setPrimaryKeyName(String PrimaryKeyName) {
        this.PrimaryKeyName = PrimaryKeyName;
    }

}
